using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using E4RPManager.Controls;

namespace E4RPManager
{
    public partial class MainRibbonForm : RibbonForm
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.OwnerWindow = this;
                clientsXtraUserControl.Controller = _controller;
                partnersXtraUserControl.Controller = _controller;
                productsXtraUserControl.Controller = _controller;                
            }
        }

        #endregion

        public MainRibbonForm()
        {
            InitializeComponent();
            // Main - Clients
            newClientBarButtonItem.ItemClick += NewClientBarButtonItemItemClick;
            newPartnerBarButtonItem.ItemClick += NewPartnerBarButtonItemItemClick;
            // Tools - Base
            generateHashCodeBarButtonItem.ItemClick += GenerateHashCodeBarButtonItemItemClick;
            generateLicenseBarButtonItem.ItemClick += GenerateLicenseBarButtonItemItemClick;
            //
            exitBarButtonItem.ItemClick += ExitBarButtonItemItemClick;
            Closing += MainRibbonFormClosing;
        }

        
        void NewClientBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            ribbon.SelectedPage = mainRibbonPage;
            xtraTabControl.SelectedTabPage = clientXtraTabPage;
            clientsXtraUserControl.NewClient();
        }
        
        void NewPartnerBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            ribbon.SelectedPage = mainRibbonPage;
            xtraTabControl.SelectedTabPage = partnerXtraTabPage;
            partnersXtraUserControl.NewPartner();
        }

        static void GenerateHashCodeBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var form = new GenerateHashCodeXtraForm();
            form.ShowDialog();
        }

        static void GenerateLicenseBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var form = new LicenseGenerator();
            form.ShowDialog();
        }


        void ExitBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.CloseConnections();
            Close();
        }

        void MainRibbonFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _controller.CloseConnections();
        }

    }
}